package com.itheima.springboot_condition.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class ClassCondition implements Condition {

    /**
     *
     * @param conditionContext 上下文对象，用于获取环境，IOC容器，ClassLoader对象
     * @param annotatedTypeMetadata 注解元对象，可以获取注解对象的属性值
     * @return
     */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        /*//1.需求，当导入redis坐标后创建bean
        //思路：判断redis.clients.jedis.Jedis.class是否存在
        Boolean flag = true;
        try {
            Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;*/

        //2.需求，当导入通过注解属性值Value指定坐标后创建bean
        //获取注解属性值 value
        Map<String, Object> map = annotatedTypeMetadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        //System.out.println(map);
        String[] values = (String[]) map.get("value");

        Boolean flag = true;
        try {
            for (String className : values) {
                Class<?> cls = Class.forName(className);
            }
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;
    }
}
